package pe.upc.dsd.jjjar.metropolitano;

import pe.upc.dsd.jjjar.metropolitano.services.RequestMethod;
import pe.upc.dsd.jjjar.metropolitano.services.RestClient;
import pe.upc.dsd.jjjar.rest.informacion.Response;
import pe.upc.dsd.jjjar.rest.recarga.Respuesta;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ConfirmationActivity extends Activity {

	private static final int DIALOG_CONFIRMATION = 1;
	private EditText txtCelular, txtContrasena;
	private int nroOperacion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.confirmacion);

		Bundle extra = getIntent().getExtras();
		nroOperacion = extra.getInt("nrooperacion");

		txtCelular = (EditText) findViewById(R.id.txtCelular);
		txtContrasena = (EditText) findViewById(R.id.txtContrasena);
		Button btnFinalizar = (Button) findViewById(R.id.btnFinalizar);

		btnFinalizar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(DIALOG_CONFIRMATION);
			}
		});

	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case DIALOG_CONFIRMATION:
			return new AlertDialog.Builder(ConfirmationActivity.this)
					.setIcon(R.drawable.card)
					.setTitle(R.string.confirmacion)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface d,
										int whichButton) {

									RestClient client = new RestClient(
											Constantes.SERVER_ENDPOINT + "/Recarga/rest/recarga/confirmar/" + 
													txtCelular.getText().toString() + "/" +
													txtContrasena.getText().toString() + "/" +
													nroOperacion);
									try {
										client.Execute(RequestMethod.GET);
									} catch (Exception e) {

									}

									String response = client.getResponse();

									Gson gson = new Gson();
									Respuesta responseObject = null;
									
									try{
										responseObject = gson.fromJson(response, Respuesta.class);
									}catch(JsonParseException e){}
									
									
									if (responseObject != null) {
										String estado = responseObject
												.getEstado();
										Toast.makeText(getApplicationContext(),
												responseObject.getMensaje(),
												Toast.LENGTH_SHORT).show();
									} else {

										AlertDialog.Builder builder = new AlertDialog.Builder(
												ConfirmationActivity.this);
										builder.setMessage(
												"Se produjo un error en la comunicacion con el WebService.")
												.setCancelable(false)
												.setPositiveButton(
														"Yes",
														new DialogInterface.OnClickListener() {
															public void onClick(
																	DialogInterface dialog,
																	int id) {
																ConfirmationActivity.this
																		.finish();
															}
														})
												.setNegativeButton(
														"No",
														new DialogInterface.OnClickListener() {
															public void onClick(
																	DialogInterface dialog,
																	int id) {
																dialog.cancel();
															}
														});
										AlertDialog alert = builder.create();

									}
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked Cancel so do some stuff */
								}
							}).create();
		}
		return null;
	}

}
